package MyDisplayObjects
{
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.text.*;
	import flash.utils.Timer;
	
	/** An object of this class is used to provide a text field that appears in a static box (round rect).  When text is added the screen flashes */
	public class TextFeedbackDisplay extends Sprite
	{
		private var feedbackTField:TextField;
		private var backgroundColor:uint, textColor:uint;
		private var timer:Timer;
		private var timerCount:uint = 0;
		private var WIDTH:Number, HEIGHT:Number, ELLIPSE_WIDTH:Number, ELLIPSE_HEIGHT:Number; // the original height and width
		
		public function TextFeedbackDisplay(width:Number, height:Number, ellipseWidth:Number, ellipseHeight:Number, fontSize:Number, textColor:uint, backgroundColor:uint, text:String="")
		{
			WIDTH = width; HEIGHT = height;
			ELLIPSE_WIDTH = ellipseWidth; ELLIPSE_HEIGHT = ellipseHeight;
			this.backgroundColor = backgroundColor;
			this.textColor = textColor;
			this.graphics.lineStyle(width/200, 0x888888);
			this.graphics.beginFill(backgroundColor);
			this.graphics.drawRoundRect(0, 0, width, height, ellipseWidth, ellipseHeight);
			this.graphics.endFill();
			feedbackTField = new TextField();
			feedbackTField.defaultTextFormat = new TextFormat("Arial", fontSize, textColor);
			feedbackTField.selectable = false;
			feedbackTField.mouseEnabled = false;
			feedbackTField.wordWrap = true;
			feedbackTField.text = text;
			feedbackTField.width = this.width - this.width/10;
			feedbackTField.height = this.height;
			feedbackTField.x = this.width/20;
			feedbackTField.autoSize = "left";
			//feedbackTField.y = sentenceDisplay.height/10;			
			this.addChild(feedbackTField);			
		}
		
		//////////////////////////// FEEDBACK                               ////////////////////////////////////////
		/** Add a new string of text to feedback display, by default flash the screen to indicate that new feedback has been given */
		public function newFeedback(text:String, flash:Boolean=true):void
		{
			if (timer == null)
			{
				feedbackTField.text = text;
				feedbackTField.autoSize = "left";
				timerCount = 0;
				timer = new Timer(500, 8);
				timer.addEventListener( TimerEvent.TIMER, handleTimer);
				timer.addEventListener( TimerEvent.TIMER_COMPLETE, handleTimerComplete);
				timer.start();
			}
		}
			private function handleTimer(event:TimerEvent):void
			{
				if (timerCount % 2 == 0)
				{
					this.graphics.lineStyle(WIDTH/200, 0x888888);
					this.graphics.beginFill(0xFFFF88);
					this.graphics.drawRoundRect(0, 0, WIDTH, HEIGHT, ELLIPSE_WIDTH, ELLIPSE_HEIGHT);
					this.graphics.endFill();
					
				} else
				{
					this.graphics.lineStyle(WIDTH/200, 0x888888);
					this.graphics.beginFill(backgroundColor);
					this.graphics.drawRoundRect(0, 0, WIDTH, HEIGHT, ELLIPSE_WIDTH, ELLIPSE_HEIGHT);
					this.graphics.endFill();
				}
				timerCount++;
			}
			private function handleTimerComplete(event:TimerEvent):void
			{
				timer.stop();
				timer.removeEventListener( TimerEvent.TIMER, handleTimer);
				timer.removeEventListener( TimerEvent.TIMER_COMPLETE, handleTimerComplete);	
				timer = null;
				// make sure we return to original color
				this.graphics.lineStyle(WIDTH/200, 0x888888);
				this.graphics.beginFill(backgroundColor);
				this.graphics.drawRoundRect(0, 0, WIDTH, HEIGHT, ELLIPSE_WIDTH, ELLIPSE_HEIGHT);
				this.graphics.endFill();
			}
	}
}